import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import type { Menu, Permission, Role } from '@/types/api'
import api from '@/utils/api'

export const usePermissionStore = defineStore('permission', () => {
  // 状态
  const permissions = ref<Permission[]>([])
  const roles = ref<Role[]>([])
  const menus = ref<Menu[]>([])
  const userMenus = ref<Menu[]>([])
  const loading = ref(false)
  
  // 计算属性
  const permissionCodes = computed(() => 
    permissions.value.map(p => p.code)
  )
  
  const roleCodes = computed(() => 
    roles.value.map(r => r.code)
  )
  
  // 操作
  const fetchPermissions = async () => {
    try {
      loading.value = true
      const response = await api.get('/rbac/permissions/')
      if (response.data.success) {
        permissions.value = response.data.data.results || response.data.data
      }
    } catch (error) {
      console.error('获取权限列表失败:', error)
    } finally {
      loading.value = false
    }
  }
  
  const fetchRoles = async () => {
    try {
      loading.value = true
      const response = await api.get('/rbac/roles/')
      if (response.data.success) {
        roles.value = response.data.data.results || response.data.data
      }
    } catch (error) {
      console.error('获取角色列表失败:', error)
    } finally {
      loading.value = false
    }
  }
  
  const fetchMenus = async () => {
    try {
      loading.value = true
      const response = await api.get('/rbac/menus/')
      if (response.data.success) {
        menus.value = response.data.data.results || response.data.data
      }
    } catch (error) {
      console.error('获取菜单列表失败:', error)
    } finally {
      loading.value = false
    }
  }
  
  const fetchUserMenus = async () => {
    try {
      loading.value = true
      const response = await api.get('/rbac/menus/user_menus/')
      if (response.data.success) {
        userMenus.value = response.data.data
      }
    } catch (error) {
      console.error('获取用户菜单失败:', error)
    } finally {
      loading.value = false
    }
  }
  
  // 权限检查函数
  const hasPermission = (permissionCode: string): boolean => {
    return permissionCodes.value.includes(permissionCode)
  }
  
  const hasAnyPermission = (permissionCodes: string[]): boolean => {
    return permissionCodes.some(code => hasPermission(code))
  }
  
  const hasAllPermissions = (permissionCodes: string[]): boolean => {
    return permissionCodes.every(code => hasPermission(code))
  }
  
  const hasRole = (roleCode: string): boolean => {
    return roleCodes.value.includes(roleCode)
  }
  
  const hasAnyRole = (roleCodes: string[]): boolean => {
    return roleCodes.some(code => hasRole(code))
  }
  
  // 清除数据
  const clearPermissionData = () => {
    permissions.value = []
    roles.value = []
    menus.value = []
    userMenus.value = []
  }
  
  // 初始化权限数据
  const initializePermissions = async () => {
    await Promise.all([
      fetchPermissions(),
      fetchRoles(),
      fetchMenus(),
      fetchUserMenus()
    ])
  }
  
  return {
    // 状态
    permissions,
    roles,
    menus,
    userMenus,
    loading,
    
    // 计算属性
    permissionCodes,
    roleCodes,
    
    // 操作
    fetchPermissions,
    fetchRoles,
    fetchMenus,
    fetchUserMenus,
    hasPermission,
    hasAnyPermission,
    hasAllPermissions,
    hasRole,
    hasAnyRole,
    clearPermissionData,
    initializePermissions
  }
})
